﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Media.Imaging;
using System.Drawing;

namespace t_DTBuilderLib
{
    class TestUtilities
    {
        /// <summary>
        /// Compare byte-by-byte the two files. Return false if they aren't equal!
        /// </summary>
        /// <param name="file1"></param>
        /// <param name="file2"></param>
        /// <returns></returns>
        public static bool CompareBinaryFiles(FileInfo file1, FileInfo file2)
        {
            int bytenumber = 0;
            using (BinaryReader br1 = new BinaryReader(file1.OpenRead()))
            {
                using (BinaryReader br2 = new BinaryReader(file2.OpenRead()))
                {
                    while (true)
                    {
                        byte data1 = 0, data2 = 0;
                        bool data1_eof = false;
                        bool data2_eof = false;
                        try
                        {
                            data1 = br1.ReadByte();
                        }
                        catch
                        {
                            data1_eof = true;
                        }
                        try
                        {
                            data2 = br2.ReadByte();
                        }
                        catch
                        {
                            data2_eof = true;
                        }
                        bytenumber += 1;

                        if (data1_eof && data2_eof)
                        {
                            /// Hey - it worked! They are the same!
                            break;
                        }
                        /// Reasons that things might not be so good...
                        if (data1_eof || data2_eof)
                        {
                            return false;
                        }
                        if (data1 != data2)
                        {
                            return false;
                        }
                    }

                    br2.Close();
                }
                br1.Close();
            }
            return true;
        }

        /// <summary>
        /// Helper code to return the pixel size of an image.
        /// </summary>
        /// <param name="f"></param>
        /// <returns>x and y in a 2-thingy array</returns>
        internal static int[] GetImageSize(FileInfo f)
        {
            BitmapDecoder dc = BitmapDecoder.Create(new Uri(f.FullName), BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);

            int[] pixels = new int[2];
            
            pixels[0] = (int) (dc.Frames[0].Width/96.0 * dc.Frames[0].DpiX);
            pixels[1] = (int) (dc.Frames[0].Height/96.0 * dc.Frames[0].DpiY);

            return pixels;
        }

        /// <summary>
        /// Get the color of a particular pixel.
        /// </summary>
        /// <param name="p"></param>
        /// <param name="p_2"></param>
        /// <param name="f"></param>
        /// <returns></returns>
        internal static System.Drawing.Color GetPixelColor(int x, int y, FileInfo f)
        {
            Bitmap uriImage = new Bitmap(f.FullName);
            return uriImage.GetPixel(x, y);
        }

        /// <summary>
        /// Look at the bitmap image in the rect (x0,y0) -> (x1, y1) and check to see if any
        /// pixels are not of color color. Return false if that is the case.
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <param name="x0"></param>
        /// <param name="y0"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="color"></param>
        /// <returns>True if all pixels in specified rect are of the color color</returns>
        internal static bool AllPixelsAreOfColor(FileInfo fileInfo, int x0, int y0, int x1, int y1, Color color)
        {
            Bitmap image = new Bitmap(fileInfo.FullName);
            for (int x = x0; x <= x1; x++)
            {
                for (int y = y0; y <= y1; y++)
                {
                    if (image.GetPixel(x, y).ToArgb() != color.ToArgb())
                    {
                        return false;
                    }
                }
            }

            return true;
        }
    }
}
